//
//  Video.m
//  TheAVPlayer
//
//  Created by hbl on 16/2/3.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "Video.h"

@interface Video()
- (IBAction)playBtn:(id)sender;
- (IBAction)fullBtn:(id)sender;
- (IBAction)sliderChanged:(id)sender;
- (IBAction)startSlider:(id)sender;
- (IBAction)endSlider:(id)sender;

@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIView *toolView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UIButton *fullButton;



@property (nonatomic, strong)AVPlayerLayer *myLayer;
@property (nonatomic, assign)BOOL isShowTool;
@property (nonatomic, strong)NSTimer *progressTimer;
@property (nonatomic, assign)BOOL isFull;
@end

@implementation Video
+ (instancetype)playVideo
{
    return [[[NSBundle mainBundle]loadNibNamed:@"Video" owner:self options:nil]lastObject];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib
{
    self.myPlayer = [[AVPlayer alloc]init];
    self.myLayer = [AVPlayerLayer playerLayerWithPlayer:self.myPlayer];
    [self.img.layer addSublayer:self.myLayer];
    self.toolView.alpha = 0;
    self.isShowTool = NO;
    [self.slider setThumbImage:[UIImage imageNamed:@"thumbImage"] forState:UIControlStateNormal];
    [self.slider setMaximumTrackImage:[UIImage imageNamed:@"MaximumTrackImage"] forState:UIControlStateNormal];
    [self.slider setMinimumTrackImage:[UIImage imageNamed:@"MinimumTrackImage"] forState:UIControlStateNormal];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.img addGestureRecognizer:tap];
    
    self.playButton.selected = YES;
    self.isFull = NO;
}

- (void)addTimer
{
    self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateProgressInfo) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.progressTimer forMode:NSRunLoopCommonModes];
}

- (void)removeTimer
{
    [self.progressTimer invalidate];
    self.progressTimer = nil;
}

- (void)updateProgressInfo
{
    NSTimeInterval current = CMTimeGetSeconds(self.myPlayer.currentTime);
    NSTimeInterval duration = CMTimeGetSeconds(self.myPlayer.currentItem.duration);
    self.slider.value = current/duration;
    self.timeLabel.text = [self getCurrentTime];
    
    
}

- (void)setItem:(AVPlayerItem *)item
{
    _item = item;
    [self.myPlayer replaceCurrentItemWithPlayerItem:_item];
    [self.myPlayer play];
    [self addTimer];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.myLayer.frame = self.bounds;
}
- (IBAction)playBtn:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (btn.selected) {
        [self.myPlayer pause];
        btn.selected = !btn.selected;
        if (!self.isFull) {
            [self removeFromSuperview];
        }
        
    }else{
        [self.myPlayer play];
        btn.selected = !btn.selected;
    }
}

- (IBAction)fullBtn:(id)sender {
    [self.delegate toFull:self.isFull];
    self.isFull = !self.isFull;
    
}

- (IBAction)sliderChanged:(id)sender {
       NSTimeInterval duration = CMTimeGetSeconds(self.myPlayer.currentItem.duration);
       NSTimeInterval current = CMTimeGetSeconds(self.myPlayer.currentItem.duration)*self.slider.value;
    self.timeLabel.text = [self stringWithTime:duration currentTime:current];
   
}

- (IBAction)startSlider:(id)sender {
    [self removeTimer];
}

- (IBAction)endSlider:(id)sender {
    [self addTimer];
    NSTimeInterval current = CMTimeGetSeconds(self.myPlayer.currentItem.duration)*self.slider.value;
    [self.myPlayer seekToTime:CMTimeMakeWithSeconds(current, NSEC_PER_SEC) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
    [self.myPlayer play];
    self.playButton.selected = YES;
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    if (self.isShowTool) {
        self.toolView.alpha = 0;
        self.isShowTool = !self.isShowTool;
    }else{
        self.toolView.alpha = 1;
        self.isShowTool = !self.isShowTool;

    }
}
- (NSString *)getCurrentTime
{
    NSTimeInterval duration = CMTimeGetSeconds(self.myPlayer.currentItem.duration);
    NSTimeInterval current = CMTimeGetSeconds(self.myPlayer.currentTime);
    return [self stringWithTime:duration currentTime:current];
}

- (NSString *)stringWithTime:(NSTimeInterval)duration currentTime:(NSTimeInterval)currentTime{
    NSInteger cM = currentTime/60;
    NSInteger cS = (NSInteger)currentTime%60;
    
    NSInteger dM = duration/60;
    NSInteger dS = (NSInteger) duration % 60;
    return [NSString stringWithFormat:@"%ld:%ld/%ld:%ld",cM,cS,dM,dS];
}

- (void)dealloc{
    NSLog(@"gun");
}
@end
